// Copyright (c)  YISH. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using NSQL.Expressions;
using NSQL.Expressions.Functions;

namespace NSQL.Extensions
{
    public static class DecodeFunctionExtensions
    {
        public static SimpleMatchExpression ToSimpleMatchExpression(this IDecodeFunction decodeFunction)
        {
            if (!decodeFunction.IsValid) return null;
            var match = new SimpleMatchExpression {Comparison = decodeFunction.Items[0]};
            for (var i = 1; i < decodeFunction.Items.Count; i++)
            {
                var a = decodeFunction.Items[i];
                if (++i < decodeFunction.Items.Count)
                {
                    match.Matches.Add(new MatchPair<IExpression, IExpression>()
                    {
                        When = a,
                        Then = decodeFunction.Items[i]
                    });
                }
                else
                {
                    match.Default = a;
                }
            }
            return match;
        }
    }
}